# Figure F1: Comparison of Covid-19 unemployment effects across Asian and African number and presence of hate crimes

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)
library(dplyr)

## Combine Asian vs. African and intensive vs. extensive effects in one graph
# Intensive margin

if (!file.exists('./output/out_econ_con_trend_all.RData') &
    !file.exists('./output/afout_econ_con_trend_all.RData') &
    !file.exists('./output/out_econ_con_trend_all_extensive.RData') &
    !file.exists('./output/afout_econ_con_trend_all_extensive.RData')) {
  source('./code/figuref1reg.R')
}

# Asian effects
out_econ_con_trend_all <- get(load('./output/out_econ_con_trend_all.RData'))
# African effects
afout_econ_con_trend_all <- get(load('./output/afout_econ_con_trend_all.RData'))


# Extensive margin
loadRData <- function(fileName){
  #loads an RData file, and returns it
  load(fileName)
  get(ls()[ls() != "fileName"])
}
out_econ_con_trend_all_extensive <- loadRData('./output/out_econ_con_trend_all_extensive.RData')
afout_econ_con_trend_all_extensive <- loadRData('./output/afout_econ_con_trend_all_extensive.RData')

out <- rbind(
  tidy(out_econ_con_trend_all) %>% filter(term=='covid_econ') %>% select(estimate, conf.low, conf.high),
  tidy(out_econ_con_trend_all_extensive) %>% filter(term=='covid_econ') %>% select(estimate, conf.low, conf.high),
  tidy(afout_econ_con_trend_all) %>% filter(term=='covid_econ') %>% select(estimate, conf.low, conf.high),
  tidy(afout_econ_con_trend_all_extensive) %>% filter(term=='covid_econ') %>% select(estimate, conf.low, conf.high)
)

types <- data.frame(treat = c(rep('Asian Crimes',2), rep('African Crimes', 2)),
                    group = c('Crime Rate','Crime Presence','Crime Rate','Crime Presence'))

out <- cbind(out,types)
out$group <- factor(out$group, levels = c('Crime Rate','Crime Presence'))
out$treat <- factor(out$treat, levels = c('Asian Crimes', 'African Crimes'))

out %>% ggplot() + aes(x = treat, y = estimate, group=treat, color=group) +
  geom_point(position=position_jitterdodge(seed=246)) +
  geom_hline(yintercept = 0, colour='#999999', linetype='longdash') + 
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), position =position_jitterdodge(seed=246)) +
  scale_y_continuous('Unemployment effects of Covid-19 on hate crime rate', sec.axis = sec_axis(~ . *3, name = 'Unemployment effects of Covid-19 on hate crime presence')) +
  scale_color_grey(end = 0.6) +
  theme(
    legend.position="bottom", legend.title = element_blank(), legend.text = element_text(size = 12),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1)), axis.text.x=element_text(size=12),axis.text.y=element_text(size=12)) +
  xlab(NULL)
ggsave('./asian_african_intensive_extensive_effects.pdf', width=6, height=4.85)